#include "DaterMainGroupManagerWindow.h"
#include "DaterMainGroupListWindow.h"
#include "DaterMainGroupEventWindow.h"
#include "AnimationWindowTrans.h"
#include "DaterBaseWindowManager.h"
#include "DaterGroupEditWindow.h"
#include "DaterGroupManager.h"
#include "DaterDaterWindow.h"
#include "DaterGroupEventEditWindow.h"

MainGroupManagerWindow::MainGroupManagerWindow(const CEGUI::String& id_name, CEGUI::Window* parent):
MainWindow(parent)
{
	using namespace CEGUI;

	_root = WindowManager::getSingleton().loadWindowLayout("GroupManagerWindow.layout",id_name);

	_root->setClippedByParent(false);

	_name = _root->getName().c_str();

	_root->setVisible(false);

	_parent->addChildWindow(_root);

	GroupEditWindow* groupEditWindow = new GroupEditWindow("", System::getSingleton().getGUISheet());
	BaseWindowManager::getSingleton().addBaseWindow(groupEditWindow);

	GroupEventEditWindow* groupEventEditWindow = new GroupEventEditWindow("", System::getSingleton().getGUISheet());
	BaseWindowManager::getSingleton().addBaseWindow(groupEventEditWindow);

	MainGroupListWindow* mainGroupListWindow = new MainGroupListWindow("", _root->getChild("GroupManagerWindow/GroupWindow"));
	BaseWindowManager::getSingleton().addBaseWindow(mainGroupListWindow);

	_groupWindowStack.push_back(mainGroupListWindow);

	subscribeEvents();
}

MainGroupManagerWindow::~MainGroupManagerWindow()
{
	BaseWindowManager::getSingleton().destroyBaseWindow("GroupListWindow");
	BaseWindowManager::getSingleton().destroyBaseWindow("EditGroupWindow");
	BaseWindowManager::getSingleton().destroyBaseWindow("EditGroupEventWindow");
	CEGUI::WindowManager::getSingleton().destroyWindow(_root);
}

void MainGroupManagerWindow::showWindow()
{
	RotateTransWindow* rotateTransWindow = new RotateTransWindow(_root, "rot2");
	rotateTransWindow->start();
	//_root->setVisible(true);
	//_root->activate();
}

void MainGroupManagerWindow::hideWindow()
{
	_root->deactivate();
	RotateTransWindow* rotateTransWindow = new RotateTransWindow(_root, "rot1");
	rotateTransWindow->start();
}

void MainGroupManagerWindow::subscribeEvents()
{
	using namespace CEGUI;

	WindowManager& wmgr = WindowManager::getSingleton();

	Window* exitButton = wmgr.getWindow("GroupManagerWindow/GroupManagerBar/ExitButton");
	Window* addGroupButton = wmgr.getWindow("GroupManagerWindow/GroupManagerBar/AddGroupButton");

	exitButton->subscribeEvent(PushButton::EventClicked, Event::Subscriber(&MainGroupManagerWindow::handle_back, this));
	addGroupButton->subscribeEvent(PushButton::EventClicked, Event::Subscriber(&MainGroupManagerWindow::handle_addGroup, this));
}

bool MainGroupManagerWindow::handle_back(const CEGUI::EventArgs &args)
{
	static_cast<DaterWindow*>(BaseWindowManager::getSingleton().getWindow("Root"))->backToPreviousWindow();

	return true;
}

bool MainGroupManagerWindow::handle_addGroup(const CEGUI::EventArgs& args)
{
	BaseWindowManager::getSingleton().getWindow("EditGroupWindow")->showWindow();

	return true;
}

void MainGroupManagerWindow::gotoGroupWindow(MainWindow* targetGroupWindow)
{
	std::vector<MainWindow*>::iterator iter = _groupWindowStack.begin();

	while(iter != _groupWindowStack.end())
	{
		if((*iter) == targetGroupWindow)
		{
			_groupWindowStack.erase(iter);
			break;
		}

		iter++;
	}

	_groupWindowStack.push_back(targetGroupWindow);

	targetGroupWindow->showWindow();
	_groupWindowStack[_groupWindowStack.size() - 2]->hideWindow();
}

void MainGroupManagerWindow::backToPreviousWindow()
{
	_groupWindowStack[_groupWindowStack.size() - 1]->hideWindow();
	_groupWindowStack[_groupWindowStack.size() - 2]->showWindow();

	_groupWindowStack.pop_back();
}